#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "sqlite3.h"
#include "logger.h"
//#include "database.h"

#if 1
typedef struct data
{

	char  sn[64];
	char  time[64];
	float  temp;
}st_data;
int database_open(sqlite3 **db, char *fliename);

int database_insert_data( sqlite3 *db, st_data *data);//存入数据库	

int database_query_data(sqlite3 *db, st_data *buff);//从数据库中获取数据

int database_delete_data(sqlite3 *db);//删除数据库


int database_get_id( sqlite3 *db);//存入数据库	

int database_get_count(sqlite3 *db);

int main(int argc, char **argv)
{

	st_data      data_buff;
	st_data      one_buff;
	sqlite3     *db;
        char        *errmsg;
	int          max_id;
	int          rv;

	strncpy(data_buff.sn,"ZKF0001", sizeof(data_buff.sn));
	strncpy(data_buff.time,"2022-05-07  14:30", sizeof(data_buff.time));
	data_buff.temp=20.1;

		
        if(database_open(&db, "data.db")!=0)
	{
		log_error("open() failure\n");
	}
#if 0
        if(database_insert_data(db, &data_buff)!=0)
	{
		log_error("insert_data() failure\n");
	}
#endif
#if 0
        while (1)//存入数据库	
	{

                if((rv=database_get_id( db))>0)//存入数据库	
		{
			printf("rv:%d\n", rv);
			memset(&one_buff, 0, sizeof(one_buff));

			if(database_query_data(db, &one_buff)!=0)
			{
				log_error("query_data() failure\n");
			}
			printf("1,%s/%s/%2.2f\n", one_buff.sn, one_buff.time, one_buff.temp);
			if(database_delete_data(db)!=0)
			{
				log_error("delete_data() failure\n");
			}
			printf("keep going!\n");
		}
		else if(rv<0)
		{
		        printf("database is NULL\n");	
		}
		sleep(3);
		printf("do something\n");
	}
	sqlite3_close(db);
	
	return 0;


#endif
}
#endif
int database_open(sqlite3 **db, char *filename)
{


        char                create[128];
        char               *errmsg;
	char		  **dbresult;
	int		    row=0;
	int		    column=0;

	memset(create, 0, sizeof(create));
        snprintf( create, sizeof(create), "create table if not exists client_pj(ID integer PRIMARY KEY, SN char(10), TIME char(50), TEMP char(10));");
	if(sqlite3_open(filename, db)!=0)
	{
		log_error("open()%s\n",errmsg);
	}


        if( sqlite3_exec(*db, create, NULL, NULL, &errmsg)!=0)
	{
		log_error("create()%s\n",errmsg);
		sqlite3_close(*db);
	}
	return 0;
}


int database_insert_data( sqlite3 *db, st_data *data)//存入数据库	
{
	
	char          insert[128];
        char         *errmsg;
	char	    **dbresult;
	int           row=0;
	int	      column=0;
	int           rv;

	if( !db||!data )
	{
		log_error("ERROR: Invalid input arguments\n");//无效的参数
		return -1;
	}

	memset(insert, 0, sizeof(insert));
	snprintf(insert, sizeof(insert), "insert into client_pj(SN, TIME, TEMP) values('%s', '%s', '%2.2f');", data->sn, data->time, data->temp); 
    
	if(sqlite3_exec(db, insert, NULL, NULL, &errmsg)!= 0)
	{
	    	log_error("insert()%s\n", errmsg);
	}
	return 0 ;
}

int database_query_data(sqlite3 *db, st_data *buff)//从数据库中获取一条数据
{
	char               *errmsg;
	char		  **dbresult;
	int		    row=0;
	int		    column=0;
	char                select[128];

	st_data            *p=buff;

	memset(select, 0, sizeof(select));
	snprintf(select, sizeof(select),"select * from client_pj limit 1;");

        if(sqlite3_get_table(db, select, &dbresult, &row, &column, &errmsg)!=SQLITE_OK)//为什么传的是db的值不是地址???
	{
            log_error("select()%s\n", errmsg);
        }
	strcpy(p->sn,dbresult[5]);
	strcpy(p->time,dbresult[6]);
	p->temp=atof(dbresult[7]);
	
//	printf("%s/%s/%s\n", buff->SN, buff->TIME, buff->TEMP);
	return 0;
}

int database_delete_data(sqlite3 *db)//删除数据库
{
        char               *errmsg;
	char                delete[128];
	char		  **dbresult;
	int		    row=0;
	int		    column=0;


	memset(delete, 0, sizeof(delete));
	snprintf(delete, sizeof(delete),"delete from client_pj where rowid in ( select rowid from client_pj LIMIT 1);");
#if 1
        if(sqlite3_exec(db, delete, NULL, NULL, &errmsg)!=0)//为什么传的是db的值不是地址???
	{
            log_info("delete()%s\n", errmsg);
	    sqlite3_close(db);
        }
#endif
	return 0;
}
int database_get_id( sqlite3 *db)//存入数据库	
{
	
	char          get_id[128];
        char         *errmsg;
	char	    **dbresult;
	int           row=0;
	int	      column=0;
	int           rv;

	if( !db)
	{
		log_error("ERROR: Invalid input arguments\n");//无效的参数
		return -1;
	}

	memset(get_id, 0, sizeof(get_id));
	snprintf(get_id, sizeof(get_id), "select count(*) from client_pj;"); 
	if(sqlite3_get_table(db, get_id, &dbresult, &row, &column, &errmsg)!=SQLITE_OK)//为什么传的是db的值不是地址???
	{
            log_error("delete()%s\n", errmsg);
        }

	rv=atoi(dbresult[1]);
	log_info(" %d group of data\n", rv);
	return  rv;
}
